﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using CC98.Metro.Data;
using CC98.Data;

namespace CC98.Metro.Pages.Account
{
    /// <summary>
    /// A page that displays a collection of item previews.  In the Split Application this page
    /// is used to display and select one of the available groups.
    /// </summary>
    public sealed partial class AccountList : CC98.Metro.Common.LayoutAwarePage
    {
        public AccountList()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: Assign a bindable collection of items to this.DefaultViewModel["Items"]
            this.DefaultViewModel["Items"] = (new AccountDataItem[]
            {
                new AccountDataItem(){ IsLoggedIn = true, User = new User("Lily")},
                new AccountDataItem(){ IsLoggedIn = true, User = new User("Frank")},
                new AccountDataItem(){ IsLoggedIn = false, User = new User("Jim")}
            }).GroupBy(p => p.IsLoggedIn);
        }

        private void btnNewUser_Click(object sender, RoutedEventArgs e)
        {
            // Navigate to auth page
            this.Frame.Navigate(typeof(Authentication), null);
        }

        private void btnSwitch_Click(object sender, RoutedEventArgs e)
        {
            if (this.itemGridView.SelectedItem != null)
            {
                var account = (AccountDataItem)this.itemGridView.SelectedItem;
                if (account.IsLoggedIn)
                {
                    // Switch
                    CommandHandler.SwitchUser.Execute(account.User);
                    // Try go back
                    if (this.Frame.CanGoBack)
                        this.Frame.GoBack();
                }
                else
                {
                    // Navigate to auth page
                    CommandHandler.NavigateTo(typeof(Authentication), account.User);
                }
            }
        }

        private void btnViewUser_Click(object sender, RoutedEventArgs e)
        {
            if (this.itemGridView.SelectedItem != null)
            {
                var account = (AccountDataItem)this.itemGridView.SelectedItem;
                // Navigate to account detail page
                CommandHandler.NavigateTo(typeof(AccountDetail), account.User);
            }
        }

        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            if (this.itemGridView.SelectedItem != null)
            {
                // TODO: Remove this account
            }
        }
    }
}
